package edu.java.ishop.dao.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;

import org.springframework.beans.factory.annotation.Value;

import edu.java.ishop.dao.ProductDao;
import edu.java.ishop.dao.accessors.FileDatasourceAccessor;
import edu.java.ishop.model.Product;
import edu.java.ishop.model.ProductCategory;
import edu.java.ishop.utils.ApplicationConfiguration;
@Named("fileDao")
public class ProductFileDao extends FileDatasourceAccessor implements
		ProductDao {

	
	public ProductFileDao() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean save(Product product) {
		throw new UnsupportedOperationException("save");

	}

	@Override
	public void delete(long Id) {
		throw new UnsupportedOperationException("delete");

	}

	@Override
	public Product getById(long Id) {
		throw new UnsupportedOperationException("getById");
	}

	@Override
	public List<Product> getAll() {
		List<Product> result = new ArrayList<Product>();
		try(BufferedReader reader = getReader()){
			String line = null;
			while((line = reader.readLine()) != null){
				String[] columns = line.split(",");
				Product p = new Product(Long.parseLong(columns[0]));
				p.setName(columns[1]);
				p.setPrice(new BigDecimal(columns[2]));
				p.setDescription(columns[3]);
				ProductCategory pc = new ProductCategory(0l,columns[4]);
				//pc.setName(columns[4]);
				p.setCategory(pc);
				result.add(p);
			}
		}catch(IOException e){
			e.printStackTrace();
			//TODO:Handle
		}
		
		return result;
	}

	@Override
	public List<Product> getByCategoty(ProductCategory category) {
		throw new UnsupportedOperationException("getByCategoty");

	}

	//
	@Value("${products.file.source}")
	private String productsourceName;
	//
	@Override
	protected String getDatasourceName() {
		return productsourceName;
			//ApplicationConfiguration.getItem("products.file.source");
	
	}

	@Override
	public void delete(Product model) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Product product) {
		// TODO Auto-generated method stub
		
	}

}
